﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using prototype123.controllers;
using prototype123.models;

namespace prototype123.data_access
{
    //This class handles all data access between the application and the
    //studentterm table in the database.
    class StudentTermDAO
    {

        private SqlConnection db_connection; // = prototype123.Program.connect.GetConnection();

        public StudentTermDAO(SqlConnection connection)
        {
            db_connection = connection;
        }

        /*precondition: the system needs to get the current term that a student is enrolled in
         description: given the student's ID number (stuid), this method queries the database to
               get the term number that the student is currently in. This is done by querying for
               the MAX of that student's term numbers, as the max is the most recent term.
         postcondition: the system receives an integer representing the current term number
                of the student represented by int stuid */
        public int GetCurrentStudentTerm(int stuid)
        {
            SqlCommand id = new SqlCommand("SELECT MAX(TERM_ID) FROM STU_TERM WHERE STU_ID = " + stuid, db_connection);
            SqlDataReader read = id.ExecuteReader();
            read.Read();

            if (read.HasRows == false)
            {
                read.Close();
                return -1;
            }

            int result = Convert.ToInt32(read[0].ToString());
            read.Close();
            return result;
        }

      /*preconditions: educator has logged in and student is selected
        postcondition: educator has ended term for student
        description: this executes a command that updates the STU_TERM table
              and updates the term status for the current student to be S which
              stands for "submitted"*/
        public void EndTerm()
        {
            
            /*int stuid = SystemController.Instance.CurrentStudent.StudentID;
            int termid = GetCurrentStudentTerm(stuid);

            SqlCommand endterm = new SqlCommand("UPDATE STU_TERM SET STATUS = 'S' WHERE " +
                "STU_ID = " + stuid + " and TERM_ID = " + termid, Connector.Instance.Connection);

            endterm.ExecuteNonQuery();*/
        }

    }
}
